import unittest,requests

class weidu(unittest.TestCase):

    host='http://10.59.9.18'
    sessionId=None
    userId=None
    #登录
    def test_weidu_01_login(self):
        url=weidu.host+'/small/user/v1/login'
        json={'phone':'15385256987','pwd':'123456Aa'}
        login=requests.post(url=url,json=json)
        print(login.json())

        weidu.sessionId=login.json()['result']['sessionId']
        print(weidu.sessionId)

        weidu.userId=login.json()['result']['userId']
        print(weidu.userId)
    #商品列表
    def test_weidu_02_chaxun(self):
        url=weidu.host+'/small/commodity/v1/commodityList'
        chaxun=requests.get(url=url)
        print(chaxun.json())

    #商品详情
    commodityId=None
    price=None
    def test_weidu_03_find(self):
        url=weidu.host+'/small/commodity/v1/findCommodityDetailsById'
        headers={'userId':str(weidu.userId),'sessionId':weidu.sessionId}
        json={'commodityId':5}
        find=requests.get(url=url,headers=headers,params=json)
        print(find.json())
        weidu.commodityId=find.json()['result']['commodityId']
        print(weidu.commodityId)
        weidu.price=find.json()['result']['price']
        print(weidu.price)

    #地址列表
    addressId=None
    def test_weidu_04_receive(self):
        url=weidu.host+'/small/user/verify/v1/receiveAddressList'
        headers = {'userId': str(weidu.userId), 'sessionId': weidu.sessionId}
        receive=requests.get(url=url,headers=headers)
        print(receive.json())
        weidu.addressId=receive.json()['result'][0]['id']
        print(weidu.addressId)


    #创建订单
    def test_weidu_05_create(self):
        url=weidu.host+'/small/order/verify/v1/createOrder'
        headers = {'userId': str(weidu.userId), 'sessionId': weidu.sessionId}
        json={'orderInfo':[{"commodityId":weidu.commodityId,"amount":1}],'totalPrice':str(weidu.sessionId*2),'addressId':weidu.addressId}
        create=requests.post(url=url,headers=headers,data=json)
        print(create.json())

#执行
if __name__ == '__main__':
    unittest.main()